﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
using Microsoft.Extensions.DependencyInjection;

namespace Sdari.Api.DbModel;

public static class DbModelExtensions
{
    /// <summary>
    /// 写入Model的Extension，防止直接创建DbContext
    /// </summary>
    /// <param name="services"></param>
    /// <returns></returns>
    public static IServiceCollection AddTokenServiceDbContext(this IServiceCollection services) => services
#pragma warning disable EF1001 // Internal EF Core API usage.
        .AddDbContext<ITokenServiceContext, TokenServiceContext>(op => op
            .UseSqlServer("Name=ConnectionStrings:TokenDbConnection")
            .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
            //支持2008，不然好像Skip生成的语句会报错
            .ReplaceService<IQueryTranslationPostprocessorFactory, SqlServer2008QueryTranslationPostprocessorFactory>());
#pragma warning restore EF1001 // Internal EF Core API usage.

}
